﻿@using TD.CTS.Data.Entities
@using TD.CTS.WebUI.Models

@model Schedule
@(Html.Kendo().Grid<ScheduleProcedure>()
    .Name("ProceduresGrid")
    .Selectable(select => select.Type(GridSelectionType.Cell))
    .Columns(columns =>
    {
        columns.ForeignKey(e => e.ProcedureCode, (System.Collections.IEnumerable)ViewBag.Procedures, "Code", "Name")
            .Width(400)
            .Filterable(false)
            .HeaderTemplate(@"<span class='k-link' style='text-align:right;width:200px;'>Визиты</span><span class='k-link'>Процедуры</span>")
            .HtmlAttributes(new { @class = "k-group-cell" });

        var visits = (List<ScheduleVisit>)ViewBag.Visits;
        foreach (var visit in visits)
        {
            columns.Template(e => { })
                .ClientTemplate("#= SelectedVisit(" + visit.TrialVisitID + ", data) #")
                .HeaderTemplate(string.Format(@"<input type='hidden' value='{2}'/><span class='k-link'>{0}</span><span class='k-link'>{1}</span>", visit.TrialVisitName, visit.ScheduleDate.HasValue ? visit.ScheduleDate.Value.Date.ToString("dd.MM.yyyy") : "", visit.TrialVisitID))
                .HtmlAttributes(new { style = "text-align:center;" }).HeaderHtmlAttributes(new { style = "text-align:center;" })
                .Width(100);

        }
        columns.Template(e => { }).ClientTemplate(" ").Width("100%").HtmlAttributes(new { @class = "k-group-cell" });

        //buttonsRow.Append("<th/></tr>");
    })
    .DataSource(dataSource => dataSource
       .Ajax()
       .Model(model =>
       {
           model.Id(e => e.ScheduleID);
           model.Field(e => e.ProcedureCode).Editable(false);
           model.Field(e => e.ScheduleID).DefaultValue(Model.Id);
           model.Field(e => e.TrialCode).DefaultValue(Model.TrialCode);
           model.Field(e => e.TrialCenterId).DefaultValue(Model.TrialCenterID);
           model.Field(e => e.VisitIds).DefaultValue(new List<int>());
       })
       .Read(read => read.Action("GetScheduleProcedures", "Schedules", new { ScheduleID = Model.Id })/*.Data("ProceduresGetDataRead")*/)
       .Events(events => events.Error("kendo_grid_error_handler")/*.RequestEnd("RequestEnd")*/)
    )
)

<script>
    function SelectedVisit(visitId, data) {
        if ($.inArray(visitId, data.VisitIds) !== -1) {
            return "<span class=\"k-icon k-i-tick td-activeTick\" visitId=\"" + visitId + "\" procedureCode=\"" + data.ProcedureCode + "\" title=\"Список сотрудников\"></span>";
        }
        return "";
    }



    //kendo_grid_init($("#ProceduresGrid"));
    $("#ProceduresGrid").on("click", ".td-activeTick", function () {
        var tick = $(this);
        OpenProceduereEmployeeWindow(tick.attr("visitId"), tick.attr("procedureCode"));
    });
</script>